package com.zmn.oms.admin.controller.conf.ordertag;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.data.LayuiTableDO;
import com.zmn.oms.admin.controller.OmsBaseController;
import com.zmn.oms.business.interfaces.conf.ordertag.ConfOrderTagCategoryBService;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.conf.ordertag.ConfOrderTagCategory;
import com.zmn.oms.model.entity.conf.ordertag.ConfOrderTagCategoryQuery;
import com.zmn.oms.model.vo.conf.ordertag.ConfOrderTagCategoryVO;
import com.zmn.oms.services.interfaces.conf.ordertag.ConfOrderTagCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：订单标签分类配置
 *
 * @author  liuying
 * @date   2021/08/30 16:03
 */
@Controller
@RequestMapping("/conf/ordertag/category")
public class ConfOrderTagCategoryController extends OmsBaseController {

    @Resource
    private ConfOrderTagCategoryService confOrderTagCategoryService;
    @Resource
    private ConfOrderTagCategoryBService confOrderTagCategoryBService;

    //@RequiresPermissions("/oms/conf/ordertag/category/list.action")
    @RequestMapping(value = "/list")
    public ModelAndView list(ModelAndView mv) {
        mv.setViewName("conf/ordertag/category/list");
        return mv;
    }

    /**
     * 查询列表 ajax
     */
    @RequestMapping(value = "/ajax/list")
    @ResponseBody
    public ResultDTO ajaxList(ConfOrderTagCategoryQuery query) {
        logger.info("ConfOrderTagCategoryQuery[{}]", JSON.toJSONString(query));
        List<ConfOrderTagCategoryVO> confList = confOrderTagCategoryBService.listPageByQuery(query);
        LayuiTableDO<ConfOrderTagCategoryVO> data = new LayuiTableDO();
        data.setCount(query.getTotalCount());
        data.setData(confList);
        return ResultDTO.success(data);
    }

    /**
     * 查看/编辑
     *
     * @param mv
     * @param categId
     * @return
     */
    @RequestMapping(value = "/addEdit")
    public ModelAndView detail(ModelAndView mv, Integer categId) {
        mv.setViewName("conf/ordertag/category/detail");
        ConfOrderTagCategory category;
        if (Objects.nonNull(categId)) {
            category = confOrderTagCategoryService.findCategoryById(categId);
        } else {
            category = ConfOrderTagCategory.builder().build();
        }
        mv.addObject("data", category);
        return mv;
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public ResultDTO save(@RequestBody ConfOrderTagCategory category) {
        try {
            if (Objects.nonNull(category.getCategId())) {
                category.setUpdater(super.getCurrentStaff().getRealName());
                confOrderTagCategoryBService.modifyCategoryById(category);
                return ResultDTO.success();
            } else {
                category.setCreater(super.getCurrentStaff().getRealName());
                Integer id = confOrderTagCategoryBService.addCategory(category);
                return ResultDTO.success(id);
            }
        } catch (OmsBaseException e) {
            e.printStackTrace();
            return ResultDTO.fail(e.getMessage());
        }
    }

    /**
     * 改状态
     */
    @RequestMapping(value = "/modifyStatus")
    @ResponseBody
    public ResultDTO modifyStatus(ConfOrderTagCategory category) {
        logger.info("category[{}]", category);
        category.setUpdater(super.getCurrentStaff().getRealName());
        try {
            confOrderTagCategoryBService.modifyCategoryStatusById(category);
        } catch (OmsBaseException omsBaseException) {
            return ResultDTO.fail(omsBaseException.getMessage());
        }
        return ResultDTO.success();
    }

    /**
     * 改sort
     */
    @RequestMapping(value = "/modifySort")
    @ResponseBody
    public ResultDTO modifySort(ConfOrderTagCategory category) {
        logger.info("category[{}]", category);
        category.setUpdater(super.getCurrentStaff().getRealName());
        confOrderTagCategoryBService.modifyCategorySortById(category);
        return ResultDTO.success();
    }

}
